<?php

namespace Roles\Model;

//use Zend\Db\TableGateway\TableGateway;

require_once('nusoap/nusoap.php');
use nusoap_client as SoapClient;

class PermisoTable {

/*   protected $tableGateway;

   public function __construct(TableGateway $tableGateway) {
      $this->tableGateway = $tableGateway;
   }*/

   private $client;

   public function __construct() {
      $this->client = new SoapClient('http://soap.asistencia.uca.edu.py/roles_soap.php');
   }

/*   public function fetchAll() {
      $resultSet = $this->tableGateway->select();
      return $resultSet;
   }*/

   //SOAP Client Version
   public function fetchAll() {

      $result = $this->client->call('permisotable_fetchAll',array());
      //var_dump($result);

      //convertimos la respuesta de array a objetos
      $rowset = json_decode(json_encode($result), FALSE);

      return $rowset;
   }

/*   public function getPermiso($id) {
      $id = (int) $id;
      $rowset = $this->tableGateway->select(array('id' => $id));
      $row = $rowset->current();
      if (!$row) {
         throw new \Exception("Could not find row $id");
      }
      return $row;
   }*/

   //SOAP Client Version
   public function getPermiso($id) {
      $id = (int) $id;

      $result = $this->client->call('getPermiso',array('id' => $id));

      if (empty($result)) {
         throw new \Exception("Could not find row $id");
      }

      $permiso = new Permiso();
      $rowset = json_decode(json_encode($result), FALSE);

      $data = array(
         'id' => $rowset[0]->id,
         'modulo' => $rowset[0]->modulo,
         'accion' => $rowset[0]->accion,
      );
      $permiso->exchangeArray($data);

      return $permiso;
   }

/*   public function savePermiso(Permiso $permiso) {
      $data = array(
         'modulo' => $permiso->modulo,
         'accion' => $permiso->accion,
      );

      $id = (int)$permiso->id;
      if ($id == 0) {
         $this->tableGateway->insert($data);
      } else {
         if ($this->getPermiso($id)) {
            $this->tableGateway->update($data, array('id' => $id));
         } else {
            throw new \Exception('id does not exist');
         }
      }
   }*/

   //SOAP Client Version
   public function savePermiso(Permiso $permiso) {
      $data = array(
         'id'     => $permiso->id,
         'modulo' => $permiso->modulo,
         'accion' => $permiso->accion,
      );

      $id = (int)$permiso->id;
      if ($id == 0) {
         $this->client->call('insertPermiso', $data);
      } else {
         if ($this->getPermiso($id)) {
            $this->client->call('updatePermiso', $data);
         } else {
            throw new \Exception('id does not exist');
         }
      }
   }

/*   public function deletePermiso($id) {
      $this->tableGateway->delete(array('id' => $id));
   }*/

   //SOAP Client version
   public function deletePermiso($id) {
      $this->client->call('deletePermiso', array('id' => $id));
   }

/*   public function getPermisosByPerfil($id) {
      $select  = new \Zend\Db\Sql\Select;
//SELECT P.id, P.modulo, P.accion, perfil_permiso.id AS id_perfil_permiso FROM permisos P LEFT JOIN perfil_permiso ON permisos.id = perfil_permiso.id_permiso AND perfil_permiso.id_perfil = $id ORDER BY P.id ASC

      $select->from('permisos');
      $select->columns(array('id','modulo','accion'));
      $join_expression = new \Zend\Db\Sql\Expression("permisos.id = perfil_permiso.id_permiso AND perfil_permiso.id_perfil = $id");
      $select->join('perfil_permiso',$join_expression,array('id_perfil_permiso' => 'id'),'left');
      $select->order('permisos.id');
      //echo $select->getSqlString();

      $rowset = $this->tableGateway->selectWith($select);

      return $rowset;
   }*/

   //SOAP Client Version
   public function getPermisosByPerfil($id) {
      $id = (int) $id;

      $result = $this->client->call('getPermisosByPerfil',array('id' => $id));

      $rowset = json_decode(json_encode($result), FALSE);
      return $rowset;
   }

}
